package edu.vt.ece.rt.benchmark.intruder;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Vector;

import edu.vt.ece.rt.benchmark.hashmap.Hashmap;
import edu.vt.ece.rt.dstm.Context;
import edu.vt.ece.rt.dstm.ContextDelegator;
import edu.vt.ece.rt.dstm.TMProxy;
import edu.vt.ece.rt.quorum.ClustersManager;
import edu.vt.ece.rt.quorum.Resource;
import edu.vt.ece.rt.quorum.util.AbortException;

public class Benchmark  extends edu.vt.ece.rt.benchmark.Benchmark{

	static Decoder decoderPtr = null;
	static Stream streamPtr = null;

	@Override
	protected void createLocalObjects() {

		/* create stream of packets for read quorum node */
		//		if (ClustersManager.baseClusterId == 0) {
//		Dictionary dictionaryPtr = new Dictionary();
//		streamPtr = new Stream("stream",Intruder.PARAM_DEFAULT_ATTACK);
//		long attack = Stream.stream_generate(streamPtr, dictionaryPtr, Intruder.PARAM_DEFAULT_NUM, 
//				Intruder.PARAM_DEFAULT_SEED, Intruder.PARAM_DEFAULT_LENGTH);
		InputStream file1;
		try {
			file1 = new FileInputStream("stream");
		InputStream buffer1 = new BufferedInputStream(file1);
		ObjectInput input1 = new ObjectInputStream(buffer1);
		streamPtr = (Stream)input1.readObject();
		
		System.out.println("ID is "+streamPtr.id);
		System.out.println("attack percent is "+streamPtr.percentAttack);
		System.out.println("size is "+streamPtr.packetQueuePtr.size());

		Resource.putQObject("stream", streamPtr);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		
		
		
		decoderPtr = new Decoder("decoder");        
		System.out.println("number of packets is "+streamPtr.packetQueuePtr.size());
		
		for (int i = 1; i <= Intruder.PARAM_DEFAULT_NUM; i++) {
			new FragmentList(i+"-fragment",null);
		}
	}

	@Override
	protected Object randomId() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	protected int getOperandsCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	protected void readOperation(Object... ids) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void writeOperation(Object... ids) {

		//		Context context = ContextDelegator.getNestedContext();
		//		Stream streamPtr = null;
		//		Decoder decoderPtr = null;
		//		try {
		//			streamPtr = (Stream) TMProxy.getTMProxy().read("stream", context.getContextId());
		//			decoderPtr = (Decoder) TMProxy.getTMProxy().read("decoder", context.getContextId());
		//		} catch (AbortException e) {
		//			e.printStackTrace();
		//		}

		Vector<Long> errorVectorPtr = new Vector<Long>();
		Intruder.processPackets("stream", decoderPtr, errorVectorPtr);

		System.out.println("count of flows is "+Intruder.count);
		System.out.println("Generated flows is "+Intruder.PARAM_DEFAULT_NUM);
	}

	@Override
	protected void checkSanity() {
		// TODO Auto-generated method stub

	}


	@Override
	protected String getLabel() {

		return "intruder-qr";
	}


}
